/*************************************************************************
 *                                                                       *
 * This file is part of the FreeMAPs distribution.  For full copyright   *
 * information, see COPYRIGHT and COPYING.LESSER.                        *
 *                                                                       *
 * Author:           sqbang                                              *
 * Email:            sqbang@ustc.edu                                     *
 * Date:             2012.12.20                                          *
 *                                                                       *
 *************************************************************************/

//declaration header
#include "COMMON/bdry/IrregularBoundaryConditionRange.h"

//COMMON headers
#include "COMMON/base/DefaultParameters.h"
#include "COMMON/equa/EquationDomain.h"
#include "COMMON/info/InfoManager.h"
#include "COMMON/tbox/LogMessage.h"



namespace FreeMAPs
{

IrregularBoundaryConditionRange::IrregularBoundaryConditionRange (
        const std::string& parent_name,
        const std::string& object_name,
        const bool use_structured_mesh,
        bool is_registered_in_info_manager)
    : Parent (parent_name, object_name, is_registered_in_info_manager),
      d_use_structured_mesh (use_structured_mesh),
      d_range ("")
{}



IrregularBoundaryConditionRange::~IrregularBoundaryConditionRange()
{
    this->clear();
}



void
IrregularBoundaryConditionRange::clear()
{
    d_boundary_names.clear();
}



void
IrregularBoundaryConditionRange::get_from_input (
    const InputDatabase* db)
{
    this->prepare_for_use (db);

    db->insert_allowable_key ("boundary_names");
    db->insert_allowable_key ("range");

    db->check_allowable_keys();

    /*
     * build boundary_names
     */
    {
        if (db->key_exists ("boundary_names") && db->is_string("boundary_names"))
        {
            d_boundary_names = Utilities::trans_vec_to_set (db->get_string_vector ("boundary_names"));
        }
    }
#if 0
    /*
     * build range
     */
    {
        db->check_optional_key ("range", DataType::STRING, false, false);
        if (db->key_exists ("range"))
        {
            Utilities::clear_pointer_object (d_range);
            d_range = new AnalyticFunction (this->get_full_name(), "range", "range");
            d_range->create_analytic_function (
                db->get_string ("range"),
                db,
                true, //is logic expression
                InfoManager::get_instance()->get_variable_info(),
                InfoManager::get_instance()->get_function_info());

            d_range->set_used_flag();
        }
    }
#endif
    if (db->key_exists ("range") && db->is_string("range"))
    {
        d_range = db->get_string("range");
    }
}



std::ostream&
operator << (
    std::ostream& os,
    const IrregularBoundaryConditionRange& rhs)
{
    std::ostringstream oss;

    os << "act_range\n{\n";

    /*
     * output on_level_set_boundaries
     * sometime it's region
     */
    if (!rhs.d_boundary_names.empty())
    {
        os << LogMessage::get_prefix (ONE_TAB) << "boundary_names = ";

        const int& num_in_one_line = rhs.d_boundary_names.size();
        os << LogMessage::get_log_info (
               Utilities::get_rearranged_set (rhs.d_boundary_names, true, /*1*/num_in_one_line),//make string in the same line
               static_cast<int>(std::string("boundary_names = ").size()) + ONE_TAB,
               true) << '\n';
    }

    /*
     * output range
     */
    if ( !rhs.d_range.empty() )
    {
        os << LogMessage::get_prefix (ONE_TAB) << "range = \"" << rhs.d_range << "\"\n";
    }

    os << "}";

    return os;
}

} //namespace FreeMAPs
